(function (window, doc) {

    var MySlipScroll = function (total, con, callback, topEnable) {
        this.load = false,//锁
            this.isreloadD = false,//上拉加载 --向上
            this.isreloadU = false,//下拉加载
        //全局变量，触摸开始位置  
            this.startX = 0,
            this.startY = 0,
            this.count = 0, //已加载的页数
            this.countSize = 5, //最大页数
            this.isEnd = false, //是否结束加载
            this.isEndDown = false,//是否结束下拉加载
            this.topIndex = 0,//下拉加载页码
            this.bottomIndex = 2,//上拉加载页码
            this.pageCount = total,//总页数
            this.mybody = window,
            this.ScrollObj = { contain: "", callback: function () { }, upEnable: false, resultCont: "result", syncImgCont: "syncImg", syncImg: "imgSync" },
            this.ShowDataObj = { state01: "上拉加载数据", state02: "松开手进行刷新", state03: "加载中...", state04: "没有更多数据了" };
        this.ScrollObj.contain = con;
        this.ScrollObj.callback = callback;
        this.ScrollObj.upEnable = topEnable;

    }

    MySlipScroll.prototype =
    {
        updScrollCont: function (r, sc, si) {
            if (resultCont != "") {
                this.ScrollObj.resultCont = r;
            }
            if (syncImgCont != "") {
                this.ScrollObj.syncImgCont = sc;
            }
            if (syncImg != "") {
                this.ScrollObj.syncImg = si;
            }
        },
        msgSet: function (obj) {
            this.ShowDataObj = obj;
        },
        setClearCount: function (count) {
            this.countSize = count;
        },
        handleEvent: function (e) {
            switch (e.type) {
                case "touchstart":
                    this.touchSatrtFunc(e);
                    break;
                case "touchmove":
                    this.touchMoveFunc(e);
                    break;
                case "touchend":
                    this.touchEndFunc(e);
                    break;
            }
        },
        //停止触发
        stopScroll: function (upOrDown) {
            if (upOrDown == "up") {
                this.isEnd = true;
                document.getElementById(this.ScrollObj.resultCont).innerText = this.ShowDataObj.state04;
            } else {
                this.isEndDown = true;
            }
        },
        //绑定事件  
        init: function () {
            this.mybody.addEventListener('touchstart', this, false);
            this.mybody.addEventListener('touchmove', this, false);
            this.mybody.addEventListener('touchend', this, false);
            this.getIndex();
        },
        //设定滚动容器
        setScrollBody: function (obj) {
            this.mybody = obj;
        },
        //touchstart事件  
        touchSatrtFunc: function (evt) {
            if (this.load)return;
            this.getIndex();
            try {
                var touch = evt.touches[0]; //获取第一个触点  
                var x = Number(touch.pageX); //页面触点X坐标  
                var y = Number(touch.pageY); //页面触点Y坐标  
                //记录触点初始位置  
                this.startX = x;
                this.startY = y;
                if (this.isScrollEnd()) {
                    this.isreloadD = true;
                } else {
                    this.isreloadD = false;
                }
                if (this.isScrollTop() && this.ScrollObj.upEnable) {
                    this.isreloadU = true;
                } else {
                    this.isreloadU = false;
                }
            } catch (e) {

            }
        },
        //touchmove事件，这个事件无法获取坐标  
        touchMoveFunc: function (evt) {
            if (!(document.getElementById(this.ScrollObj.syncImgCont).style.display == "none"))
                evt.preventDefault();
            try {
                var touch = evt.touches[0]; //获取第一个触点  
                var x = Number(touch.pageX); //页面触点X坐标  
                var y = Number(touch.pageY); //页面触点Y坐标
                if (this.isreloadD && this.startY - y > 0 && (document.getElementById(this.ScrollObj.syncImgCont).style.display == "none")) {
                    evt.preventDefault();
                    if (this.isEnd) return;
                    this.showImg(1);
                    this.load = true;
                    this.isreloadU = false;
                } else if (this.isreloadU && this.startY - y < 0 && (document.getElementById(this.ScrollObj.syncImgCont).style.display == "none")) {
                    evt.preventDefault();
                    if (this.isEndDown) return;
                    this.showImg(2);
                    this.isreloadD = false;
                    this.load = true;
                }
            } catch (e) {

            }
        },
        showImg: function (t) {
            if (t == 1) {
                document.getElementById(this.ScrollObj.resultCont).innerText = this.ShowDataObj.state02;
                document.getElementById(this.ScrollObj.syncImgCont).style.top = "";
                document.getElementById(this.ScrollObj.syncImgCont).style.bottom = "0px";
                document.getElementById(this.ScrollObj.syncImgCont).style.display = "block";
                if ($) {
                    $("#imgSync").animate({ "width": "60px" }, 100, function () { $("#syncImg").animate({ "bottom": "60px" }, 100); });
                } else {
                    document.getElementById(this.ScrollObj.syncImg).style.width = "60px";
                    document.getElementById(this.ScrollObj.syncImgCont).style.bottom = "60px";
                }
            } else {
                document.getElementById(this.ScrollObj.syncImgCont).style.bottom = "";
                document.getElementById(this.ScrollObj.syncImgCont).style.top = "0px";
                document.getElementById(this.ScrollObj.syncImgCont).style.display = "block";
                if ($) {
                    $("#imgSync").animate({ "width": "60px" }, 100, function () { $("#syncImg").animate({ "top": "60px" }, 100); });
                } else {
                    document.getElementById(this.ScrollObj.syncImg).style.width = "60px";
                    document.getElementById(this.ScrollObj.syncImgCont).style.top = "60px";
                }
            }
        },
        //touchend事件  
        touchEndFunc: function (evt) {
            try {
                if (this.load && (this.isreloadD || this.isreloadU)) {
                    document.getElementById(this.ScrollObj.resultCont).innerText = this.ShowDataObj.state03;
                    this.ScrollObj.callback();
                }
            } catch (e) {

            }
        },
        loadData: function (data) {
            this.load = false;
            this.count++;
            if (this.count >= this.countSize && this.ScrollObj.upEnable) {
                this.count = 0;
                if (typeof data == "string") {
                    document.getElementById(this.ScrollObj.contain).innerHTML = data;
                } else {
                    this._clearAndAdd(data, document.getElementById(this.ScrollObj.contain));
                }
                if (this.isreloadD) {
                    this.topIndex = this.bottomIndex - 1;
                    this.bottomIndex++;
                } else if (this.isreloadU) {
                    this.bottomIndex = this.topIndex + 1;
                    this.topIndex--;
                }
                scrollTo(0, 0);
            } else {
                if (this.isreloadD) {
                    this.bottomIndex++;
                    if (typeof data == "string") {
                        document.getElementById(this.ScrollObj.contain).innerHTML += data;
                    } else {
                        this._appendList(data, document.getElementById(this.ScrollObj.contain));
                    }
                } else if (this.isreloadU) {
                    this.topIndex--;
                    if (typeof data == "string") {
                        document.getElementById(this.ScrollObj.contain).innerHTML = data + document.getElementById(this.ScrollObj.contain).innerHTML;
                    } else {
                        this._insertList(data, document.getElementById(this.ScrollObj.contain));
                    }
                }
            }
            this.getIndex();
            document.getElementById(this.ScrollObj.syncImgCont).style.display = "none";
            document.getElementById(this.ScrollObj.syncImg).style.width = "0";

        },
        //获取查询页码
        getIndex: function () {
            //是否可以继续加载处理
            if (this.pageCount >= this.bottomIndex) {
                this.isEnd = false;
                document.getElementById(this.ScrollObj.resultCont).innerText = this.ShowDataObj.state01;
            } else {
                this.isEnd = true;
                document.getElementById(this.ScrollObj.resultCont).innerText = this.ShowDataObj.state04;
            }
            //下拉处理
            if (this.isEndDown && this.topIndex > 0) {
                this.isEndDown = false;
            } else if (this.topIndex <= 0) {
                this.isEndDown = true;
            }
            if (this.isreloadD) {
                if (this.pageCount >= this.bottomIndex) {
                    return this.bottomIndex;
                } else {
                    this.isEnd = true;
                    return this.pageCount;
                }
            } else if (this.isreloadU) {
                if (this.topIndex == 0) {
                    this.isEndDown = true;
                    return 1;
                }
                return this.topIndex;
            }
        },
        /******************** 
        * 取窗口滚动条高度  
        ******************/
        getScrollTop: function () {
            var scrollTop = 0;
            if (document.documentElement && document.documentElement.scrollTop) {
                scrollTop = document.documentElement.scrollTop;
            } else if (document.body) {
                scrollTop = document.body.scrollTop;
            }
            return scrollTop;
        },
        /******************** 
        * 取窗口可视范围的高度  
        *******************/
        getClientHeight: function () {
            var clientHeight = 0;
            if (document.body.clientHeight && document.documentElement.clientHeight) {
                if (document.documentElement.clientHeight > 0)
                    clientHeight = document.documentElement.clientHeight;
                else
                    clientHeight = (document.body.clientHeight < document.documentElement.clientHeight) ? document.body.clientHeight : document.documentElement.clientHeight;
            } else {
                clientHeight = (document.body.clientHeight > document.documentElement.clientHeight) ? document.body.clientHeight : document.documentElement.clientHeight;
            }
            return clientHeight;
        },
        /******************** 
        * 取文档内容实际高度  
        *******************/
        getScrollHeight: function () {
            return Math.max(document.body.scrollHeight, document.documentElement.scrollHeight);
        },
        isScrollEnd: function () {
            if (this.getScrollTop() + this.getClientHeight() >= this.getScrollHeight() - 10) {
                return true;
            } else {
                return false;
            }
        },
        isScrollTop: function () {
            return (document.body.scrollTop || document.documentElement.scrollTop) - 10 <= 0;
        },
        _appendList: function (data, contain) {
            for (var i = 0; i < data.length; i++) {
                contain.appendChild(data[i]);
            }
        },
        _insertList: function (data, contain) {
            var fc = contain.firstChild;
            for (var i = 0; i < data.length; i++) {
                contain.insertBefore(data[i], fc);
            }
        },
        _clearAndAdd: function (data, contain) {
            contain.innerHTML = "";
            this._appendList(data, contain);
        }
    }

    if (typeof exports !== 'undefined') exports.MySlipScroll = MySlipScroll;
    else window.MySlipScroll = MySlipScroll;
})(window, document);